var express = require('express');
var router = express.Router();
var User = require('../models/User');
var Content = require('../models/Content');

var responseData;
router.use(function(req, res , next){
    //每次调用api下面的这个都被初始化，code为0
    responseData = {
        code:0,
        message:''
    };
    next();
})


router.post('/user/register',function(req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    var password2 = req.body.password2;

    //用户名是否为空；
    if(username === ''){
        responseData.code =1;
        responseData.message = '用户名不能为空';
        res.json(responseData);
        return;
    }

    //密码不能为空
    if(password === ''){
        responseData.code = 2;
        responseData.message = '密码不能为空';
        res.json(responseData);
        return;
    }

    //两次密码要一样
    if(password !== password2){
        responseData.code = 3;
        responseData.message = '两次密码不一样';
        res.json(responseData);
        return;
    }

    User.findOne({
        username:username
    }).then(function(userInfo) {
        console.log(userInfo);
        if(userInfo) {
            responseData.code = 4;
            responseData.message = '用户名已经被注册了';
            res.json(responseData);
            return;
        }

        var user = new User({
            username:username,
            password:password
        });
        return user.save();
    }).then(function(newUserInfo) {
        console.log(newUserInfo);
        responseData.message = '注册成功';
        res.json(responseData);
        return;
    });
})

router.post('/user/login',function(req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    if(username == '' || password == '') {
        responseData.code = 1;
        responseData.message = '用户名和密码不能为空';
        res.json(responseData);
        return;
    }

    //查询数据库中用户名和密码的记录是否存在，如果存在则登录成功
    User.findOne({
        username:username,
        password:password
    }).then(function(userInfo) {
        if(!userInfo) {
            responseData.code = 2;
            responseData.message = '用户名或密码错误';
            res.json(responseData);
            return;
        }
        responseData.message = '登录成功';
        responseData.userInfo = {
            _id:userInfo._id,
            username:userInfo.username
        };
        //每次都会把cookies请求数据到服务端
        req.cookies.set('userInfo',JSON.stringify({
            _id:userInfo._id,
            username:userInfo.username,
            isAdmin:userInfo.isAdmin
        }));
        res.json(responseData);
        return;

    });
});

router.get('/user/logout',function(req,res) {
    req.cookies.set('userInfo', null);
    // responseData.message = '';
    res.json(responseData);
    return;
});

router.post('/comment/post',function(req,res) {
    var postData = {
        username:req.userInfo.username,
        postTime:new Date(),
        content:req.body.content
    };
    var contentId = req.body.contentId || '';

    Content.findOne({
        _id:contentId
    }).then(function(content) {
        content.comment.push(postData);
        return content.save();
    }).then(function(newContent) {
        responseData.data = newContent;
        responseData.message = '评论成功';
        res.json(responseData);
    });

});























module.exports = router; 